package interesado;
import java.awt.JobAttributes;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;

import misc.Logica;





/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class EliminarCuenta_GUI extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel lblEliminarCuenta;
	private JButton btnCancelar;
	private JButton btnAceptar;
	private JPasswordField txtPassword;
	private JLabel lblPassword;

	//Agregado
	private Interesado_GUI padre;
	private Logica logica;
	/**
	* Auto-generated main method to display this JFrame
	*/
	
	public EliminarCuenta_GUI(Interesado_GUI padre,Logica logica) {
		super();
		initGUI();
		this.padre=padre;
		this.logica=logica;
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Eliminar cuenta");
			{
				lblEliminarCuenta = new JLabel();
				getContentPane().add(lblEliminarCuenta);
				lblEliminarCuenta.setText("Eliminar Cuenta");
				lblEliminarCuenta.setBounds(98, 19, 131, 22);
				lblEliminarCuenta.setFont(new java.awt.Font("Segoe UI",0,16));
			}
			{
				lblPassword = new JLabel();
				getContentPane().add(lblPassword);
				lblPassword.setText("Password:");
				lblPassword.setBounds(19, 63, 101, 16);
				lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				txtPassword = new JPasswordField();
				getContentPane().add(txtPassword);
				txtPassword.setBounds(132, 60, 111, 23);
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar);
				btnCancelar.setText("Cancelar");
				btnCancelar.setBounds(171, 126, 86, 23);
				btnCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						cancelar();//Volvemos a la pantalla anterior
					}
				});
			}
			{
				btnAceptar = new JButton();
				getContentPane().add(btnAceptar);
				btnAceptar.setText("Aceptar");
				btnAceptar.setBounds(54, 126, 83, 23);
				btnAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						//Verificamos si escribi� bien el password
						if(logica.getPasswordConectado().equals(txtPassword.getText())){
						
							 //Confirmamos la eliminacion
							 int respuesta = JOptionPane.showConfirmDialog(null, "¿Confirma la eliminación?", "Eliminar Cuenta",
	                                JOptionPane.YES_NO_OPTION);
							
							 //Confirmacion afirmativa, sino volvemos a la pantalla
							 if(respuesta==JOptionPane.YES_OPTION){
								 eliminarCuenta();
							 }
						}
						else
							JOptionPane.showMessageDialog(null,"el password es incorrecto","Error",JOptionPane.ERROR_MESSAGE);
					}
				});
			}
			pack();
			this.setSize(339, 214);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	//Metodos agregados
	
	private void eliminarCuenta(){
			
		int res=logica.eliminarCuenta();
		if(res==-1){
			JOptionPane.showMessageDialog(null,"Se produjo un error al eliminar la cuenta de usuario. Puede que tenga solicitudes pendientes", "Error", JOptionPane.ERROR_MESSAGE);
			this.dispose();
			padre.setVisible(true);
		}
			
		else{
			this.setVisible(false);
			JOptionPane.showMessageDialog(null,"La operación se realizó correctamente", "Eliminar cuenta de usuario", JOptionPane.INFORMATION_MESSAGE);
			//Procedemos a cerrar las interfaces
			padre.cerrarVentana();
			//muere
			this.dispose();
			this.setVisible(false);
		}
	}
	
	private void cancelar(){
		padre.setVisible(true);
		this.setVisible(false);
	}

}
